vim.api.nvim_create_autocmd('LspAttach', {
    group = vim.api.nvim_create_augroup('UserLspConfig', {}),
    callback = function(ev)
        -- Enable completion manually triggered by <c-x><c-o>
        -- vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

        -- Buffer local mappings.
        local opts = { buffer = ev.buf }
        vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
        vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
        vim.keymap.set('n', 'gs', vim.lsp.buf.type_definition, opts)
        vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
        vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
        vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
        vim.keymap.set({ 'i', 'n' }, '<C-,>', vim.lsp.buf.signature_help, opts)
        vim.keymap.set({ 'n', 'v' }, '<F1>', vim.lsp.buf.code_action, opts)
        vim.keymap.set('n', '<F2>', vim.lsp.buf.rename, opts)
        vim.keymap.set('n', '<leader>ff', function()
            vim.lsp.buf.format { async = true }
        end, opts)

        vim.keymap.set('n', '<leader>wsa', vim.lsp.buf.add_workspace_folder, opts)
        vim.keymap.set('n', '<leader>wsr', vim.lsp.buf.remove_workspace_folder, opts)
        vim.keymap.set('n', '<leader>wsl', function()
            print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
        end, opts)

        local lsp_client = vim.lsp.get_client_by_id(ev.data.client_id)
        if lsp_client then
            --[[ if lsp_client.server_capabilities.inlayHintProvider then
                vim.lsp.inlay_hint.enable(true, { buf = ev.buf })
            end ]]

            -- check https://github.com/j-hui/fidget.nvim/issues/201
            lsp_client.progress = vim.ringbuf(1000) --[[@as vim.lsp.Client.Progress]]
            lsp_client.progress.pending = {}
        end
    end,
})

vim.diagnostic.config {
    severity_sort = true
}
